package GUI;
import Buildings.Buildings;
import Interfaces.*;
import Buildings.dwelling.*;
import Buildings.office.*;
import javax.swing.*;
import java.awt.Component.*;
import java.awt.*;
import java.io.*;

public class BFrame extends javax.swing.JFrame {
    //пункт меню с laf
    private JRadioButtonMenuItem jRadioButton[];
    
    //данные для обработки
    private Building build=null;
    private static UIManager.LookAndFeelInfo[] names;

    
    private void ItemStateChanged(java.awt.event.ItemEvent evt){
        for(int i=0;i<jRadioButton.length;i++){
            if(jRadioButton[i].isSelected()==true){
                String str=jRadioButton[i].getText();
                int j=0;
                while(true){
                    if(names[j].getName().equalsIgnoreCase(str)){
                        try{
                        UIManager.setLookAndFeel(names[j].getClassName());
                        SwingUtilities.updateComponentTreeUI(this);
                        } catch (ClassNotFoundException ex) {
                            java.util.logging.Logger.getLogger(BFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
                        } catch (InstantiationException ex) {
                            java.util.logging.Logger.getLogger(BFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
                        } catch (IllegalAccessException ex) {
                            java.util.logging.Logger.getLogger(BFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
                        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                            java.util.logging.Logger.getLogger(BFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
                        }
                        break;
                    }
                    j++;
                }
                break;
            }
        }
    }
    private void menuRadio(){
        names=UIManager.getInstalledLookAndFeels();
        LANDF.setText("Компонента Look&Feel");
        JRadioButtonMenuItem sample;
        jRadioButton=new JRadioButtonMenuItem[names.length];
        for(int i=0;i<jRadioButton.length;i++){
            jRadioButton[i]=new JRadioButtonMenuItem();
            jRadioButton[i].setText(names[i].getName());
            if(i==3){
                jRadioButton[i].setSelected(true);
                try{
                        UIManager.setLookAndFeel(names[i].getClassName());
                        SwingUtilities.updateComponentTreeUI(this);
                        } catch (ClassNotFoundException ex) {
                            java.util.logging.Logger.getLogger(BFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
                        } catch (InstantiationException ex) {
                            java.util.logging.Logger.getLogger(BFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
                        } catch (IllegalAccessException ex) {
                            java.util.logging.Logger.getLogger(BFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
                        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                            java.util.logging.Logger.getLogger(BFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
                        }
            }
            else jRadioButton[i].setSelected(false);
            buttonGroup.add(jRadioButton[i]);
            jRadioButton[i].addItemListener(new java.awt.event.ItemListener() {
                public void itemStateChanged(java.awt.event.ItemEvent evt) {
                    ItemStateChanged(evt);
                }
            });
            jRadioButton[i].setVisible(true);
            LANDF.add(jRadioButton[i]);
        }
    }
    public BFrame() {
        initComponents();
        menuRadio();
        this.setVisible(true);
        this.setLocation(100, 20);
        this.setLayout(null);
        monitor.setBounds(200, 0, 600, 479);
        this.add(monitor);
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup = new javax.swing.ButtonGroup();
        Pult = new javax.swing.JPanel();
        labelBuildInfo = new javax.swing.JLabel();
        labelFloorInfo = new javax.swing.JLabel();
        labelSpaceInfo = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jSeparator2 = new javax.swing.JSeparator();
        labelBuildType = new javax.swing.JLabel();
        labelBuildFloorNum = new javax.swing.JLabel();
        labelBuildArea = new javax.swing.JLabel();
        labelFloorNum = new javax.swing.JLabel();
        labelFloorSpaceNum = new javax.swing.JLabel();
        labelFloorArea = new javax.swing.JLabel();
        labelSpaceNum = new javax.swing.JLabel();
        labelSpaceRoom = new javax.swing.JLabel();
        labelSpaceArea = new javax.swing.JLabel();
        monitor = new javax.swing.JScrollPane();
        jMenuBar1 = new javax.swing.JMenuBar();
        FileOpen = new javax.swing.JMenu();
        OpenDwelling = new javax.swing.JMenuItem();
        OpenOffice = new javax.swing.JMenuItem();
        LANDF = new javax.swing.JMenu();
        Exit = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Здания");
        setResizable(false);

        Pult.setBackground(new java.awt.Color(234, 234, 234));
        Pult.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        Pult.setPreferredSize(new java.awt.Dimension(200, 500));

        labelBuildInfo.setText("Информация о здании");
        labelBuildInfo.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        labelFloorInfo.setText("Информация о этаже");
        labelFloorInfo.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        labelSpaceInfo.setText("Информация о помещении");
        labelSpaceInfo.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        labelBuildType.setText("Тип здания");

        labelBuildFloorNum.setText("Количество этажей");

        labelBuildArea.setText("Общая площадь");

        labelFloorNum.setText("Номер этажа");

        labelFloorSpaceNum.setText("Количество помещений");

        labelFloorArea.setText("Общая площадь");

        labelSpaceNum.setText("Номер помещения");

        labelSpaceRoom.setText("Количество комнат");

        labelSpaceArea.setText("Общая площадь");

        javax.swing.GroupLayout PultLayout = new javax.swing.GroupLayout(Pult);
        Pult.setLayout(PultLayout);
        PultLayout.setHorizontalGroup(
            PultLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(PultLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(PultLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(PultLayout.createSequentialGroup()
                        .addGroup(PultLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jSeparator2)
                            .addGroup(PultLayout.createSequentialGroup()
                                .addGroup(PultLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(labelBuildInfo)
                                    .addComponent(labelFloorInfo)
                                    .addComponent(labelSpaceInfo)
                                    .addGroup(PultLayout.createSequentialGroup()
                                        .addGap(10, 10, 10)
                                        .addGroup(PultLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(labelBuildFloorNum)
                                            .addComponent(labelBuildType)
                                            .addComponent(labelBuildArea))))
                                .addGap(0, 0, Short.MAX_VALUE)))
                        .addContainerGap())
                    .addGroup(PultLayout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addGroup(PultLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(labelSpaceNum)
                            .addComponent(labelFloorSpaceNum)
                            .addComponent(labelFloorNum)
                            .addComponent(labelFloorArea)
                            .addComponent(labelSpaceRoom)
                            .addComponent(labelSpaceArea))
                        .addGap(0, 58, Short.MAX_VALUE))))
        );
        PultLayout.setVerticalGroup(
            PultLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(PultLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelBuildInfo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelBuildType)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelBuildFloorNum)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelBuildArea)
                .addGap(53, 53, 53)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelFloorInfo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelFloorNum)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelFloorSpaceNum)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelFloorArea)
                .addGap(68, 68, 68)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelSpaceInfo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelSpaceNum)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelSpaceRoom)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelSpaceArea)
                .addContainerGap(79, Short.MAX_VALUE))
        );

        monitor.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        monitor.setPreferredSize(new java.awt.Dimension(600, 400));

        FileOpen.setText("Открыть");

        OpenDwelling.setText("Открыть Dwelling");
        OpenDwelling.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                OpenDwellingActionPerformed(evt);
            }
        });
        FileOpen.add(OpenDwelling);

        OpenOffice.setText("Открыть Office");
        OpenOffice.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                OpenOfficeActionPerformed(evt);
            }
        });
        FileOpen.add(OpenOffice);

        jMenuBar1.add(FileOpen);

        LANDF.setText("Look&Feel");
        jMenuBar1.add(LANDF);

        Exit.setText("Выход");
        Exit.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                ExitMouseClicked(evt);
            }
        });
        jMenuBar1.add(Exit);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(Pult, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(monitor, javax.swing.GroupLayout.PREFERRED_SIZE, 251, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 343, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(Pult, javax.swing.GroupLayout.DEFAULT_SIZE, 479, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addComponent(monitor, javax.swing.GroupLayout.PREFERRED_SIZE, 344, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void OpenDwellingActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OpenDwellingActionPerformed
        JFileChooser fileOpen=new JFileChooser();
        int ret = fileOpen.showDialog(null, "Открыть файл");
        if (ret == JFileChooser.APPROVE_OPTION) {//Выбрали файл
            try{
            File f=fileOpen.getSelectedFile();
            Buildings.setBuildingFactory(new DwellingFactory());
            build=Buildings.readBuilding(new FileReader(f));//прочитали здание
            }catch(IOException ex){
                JOptionPane.showMessageDialog(null, "Ощибка открытия файла!", "Ошибка", JOptionPane.ERROR_MESSAGE);
                return;
            }
            this.labelBuildType.setText("Тип здания: Dwelling");
            this.labelBuildArea.setText("Общая площадь: "+Double.toString(build.getHowMuchAreaInBuilding()));
            this.labelBuildFloorNum.setText("Количество этажей: "+Integer.toString(build.getHowMuchFloorInBuilding()));
            BuildingPanel gh=new BuildingPanel(build,this);
            monitor.getViewport().add(gh);         
        }
    }//GEN-LAST:event_OpenDwellingActionPerformed
    private void OpenOfficeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OpenOfficeActionPerformed
        JFileChooser fileOpen=new JFileChooser();
        int ret = fileOpen.showDialog(null, "Открыть файл");
        if (ret == JFileChooser.APPROVE_OPTION) {//Нашли требуемый файл
            try{
            File f=fileOpen.getSelectedFile();
            Buildings.setBuildingFactory(new OfficeFactory());
            build=Buildings.readBuilding(new FileReader(f));//прочитали здание
            }catch(IOException ex){
                JOptionPane.showMessageDialog(null, "Ощибка открытия файла!", "Ошибка", JOptionPane.ERROR_MESSAGE);
                return;
            }
            this.labelBuildType.setText("Тип здания: Office");
            this.labelBuildArea.setText("Общая площадь: "+Double.toString(build.getHowMuchAreaInBuilding()));
            this.labelBuildFloorNum.setText("Количество этажей: "+Integer.toString(build.getHowMuchFloorInBuilding()));
            BuildingPanel gh=new BuildingPanel(build,this);
            monitor.getViewport().add(gh);
        }
    }//GEN-LAST:event_OpenOfficeActionPerformed
    private void ExitMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ExitMouseClicked
        System.exit(0);
    }//GEN-LAST:event_ExitMouseClicked

    public static void main(String args[]) {
//        /* Set the Nimbus look and feel */
//        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
//        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
//         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
//         */
//        try {
//            names=UIManager.getInstalledLookAndFeels();
//            UIManager.setLookAndFeel(names[0].getClassName());
////            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
////                if ("Nimbus".equals(info.getName())) {
////                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
////                    break;
////                }
////            }
//        } catch (ClassNotFoundException ex) {
//            java.util.logging.Logger.getLogger(BFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (InstantiationException ex) {
//            java.util.logging.Logger.getLogger(BFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (IllegalAccessException ex) {
//            java.util.logging.Logger.getLogger(BFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
//            java.util.logging.Logger.getLogger(BFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        }
//        //</editor-fold>
//
//        /* Create and display the form */
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                new BFrame().setVisible(true);
//            }
//        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu Exit;
    private javax.swing.JMenu FileOpen;
    private javax.swing.JMenu LANDF;
    private javax.swing.JMenuItem OpenDwelling;
    private javax.swing.JMenuItem OpenOffice;
    private javax.swing.JPanel Pult;
    private javax.swing.ButtonGroup buttonGroup;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    public javax.swing.JLabel labelBuildArea;
    public javax.swing.JLabel labelBuildFloorNum;
    private javax.swing.JLabel labelBuildInfo;
    public javax.swing.JLabel labelBuildType;
    public javax.swing.JLabel labelFloorArea;
    private javax.swing.JLabel labelFloorInfo;
    public javax.swing.JLabel labelFloorNum;
    public javax.swing.JLabel labelFloorSpaceNum;
    public javax.swing.JLabel labelSpaceArea;
    private javax.swing.JLabel labelSpaceInfo;
    public javax.swing.JLabel labelSpaceNum;
    public javax.swing.JLabel labelSpaceRoom;
    private javax.swing.JScrollPane monitor;
    // End of variables declaration//GEN-END:variables
}
